package com.example.wangzh.developkit.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;

/**
 * @author: zhwang37
 * @date: 2021/9/16 9:53
 * @email:zhwang37@iflytek.com
 * @describe: 用来测试汉字、英语字体与baseline之间位置关系
 */
@SuppressWarnings({"unused", "RedundantSuppression"})
public class TextViewTest extends AppCompatTextView {
    private final Paint mPaint = new Paint();
    private final Path path = new Path();

    public TextViewTest(Context context) {
        super(context);
        init(context, null, 0);
    }

    public TextViewTest(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public TextViewTest(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        mPaint.setColor(Color.GREEN);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        String text = getText().toString();
        canvas.drawText(text, 20, 100, getPaint());
        path.reset();
        float textWidth = getPaint().measureText(text);
        path.moveTo(20, 100);
        path.lineTo(20 + textWidth, 100);
        canvas.drawPath(path, mPaint);
        canvas.restore();
    }
}

